#ifndef LINKEDLISTWINDOW_H
#define LINKEDLISTWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QPainter>
#include <QPen>
#include <QFont>
#include <QColor>
#include <QTextEdit>
#include <QSplitter>
#include <QDialog>
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QSpinBox>
#include <QGroupBox>
#include <QMessageBox>
#include <QCoreApplication>
#include <QLabel>
#include <QFrame>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>

// 链表节点结构
struct ListNode {
    int data;
    ListNode* next;
    ListNode(int val) : data(val), next(nullptr) {}
};

// 绘图节点结构
struct DrawNode {
    QRect rectLeft;
    QRect rectRight;
    QPoint point;
    int value;
    bool isCurrent;
    bool isPrevious;
};

// 可视化组件类
class LinkedListVisualWidget : public QWidget
{
    Q_OBJECT
public:
    explicit LinkedListVisualWidget(QWidget *parent = nullptr);
    ~LinkedListVisualWidget();
    void setLinkedListData(ListNode* head, ListNode* currentNode, ListNode* previousNode,
                           ListNode* newNode, bool showNewNode, bool breakConnection,
                           ListNode* breakNode, const QString& operationType,
                           int animationStep, int targetPosition);

protected:
    void paintEvent(QPaintEvent *event) override;

private:
    ListNode* m_head;
    ListNode* m_currentNode;
    ListNode* m_previousNode;
    ListNode* m_newNode;
    bool m_showNewNode;
    bool m_breakConnection;
    ListNode* m_breakNode;
    QString m_operationType;
    int m_animationStep;
    int m_targetPosition;

    // 使用原生数组替代 QList<DrawNode>
    DrawNode* m_drawNodes;      // 动态分配的数组
    int m_drawNodesSize;        // 当前节点数量
    int m_drawNodesCapacity;    // 数组容量

    QRect m_nullRect;

    void computeDrawArea();
    void drawNode(QPainter& painter, const DrawNode& node, int index);
    void drawNewNode(QPainter& painter);
};

// 主窗口类
class LinkedListWindow : public QMainWindow
{
    Q_OBJECT

public:
    LinkedListWindow(QWidget *parent = nullptr);
    ~LinkedListWindow();

signals:
    void returnToMainMenu();

private slots:
    void onReturnToMainMenu();
    void onInputData();
    void onInsert();
    void onRemove();
    void onSearch();
    void onClear();
    void onAnimationStep();

private:
    void setupUI();
    void updateDisplay();
    void clearList();
    void resetAllHighlights();
    void startInsertAnimation(int value, int position);
    void startRemoveAnimation(int value);
    void startSearchAnimation(int value);
    void handleInsertStep();
    void handleRemoveStep();
    void handleSearchStep();

    // 链表数据
    ListNode* m_head;
    int m_size;

    // 动画控制
    QTimer* m_animationTimer;
    int m_animationStep;
    int m_currentValue;
    int m_targetPosition;
    int m_currentPosition;

    // 可视化状态
    ListNode* m_currentNode;
    ListNode* m_previousNode;
    bool m_found;
    ListNode* m_newNode;
    bool m_showNewNode;
    bool m_breakConnection;
    ListNode* m_breakNode;
    bool m_deleteSuccess;
    QString m_operationType;

    // 临时指针
    ListNode* m_tempNode;

    // UI组件
    QWidget* m_visualizationWidget;
    QTextEdit* m_display;
};

#endif // LINKEDLISTWINDOW_H
